package com.topscore.uke.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXMiniProgramObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.io.ByteArrayOutputStream;

public class WXNativeShareUtil {
    private Context mContext;

    private int mShareScene;

    private int miniProgramType = WXMiniProgramObject.MINIPROGRAM_TYPE_PREVIEW;

    private IWXAPI api;

    public WXNativeShareUtil(Context context, int shareScene) {
        this.mContext = context;
        this.mShareScene = shareScene;
        api = WXAPIFactory.createWXAPI(context, "wx03192d0b29e18848");
    }

    public void shareMiniPro(String thumbUrl, String productId, String userName, String title) {
        WXMiniProgramObject miniProgramObject = new WXMiniProgramObject();
        miniProgramObject.webpageUrl = "http://www.qq.com";
        miniProgramObject.miniprogramType = miniProgramType;
        miniProgramObject.userName = userName;
        StringBuffer sharePath = new StringBuffer();
        sharePath.append("pages/product/detail/detail")
                .append("?")
                .append("id=").append(productId)
                .append("&")
                .append("shareid=").append(UserInfoManager.getUserInfo().getLoginName());
        miniProgramObject.path = sharePath.toString();
        ImageSize imageSize = new ImageSize(200, 200).scale(1.2f);
        ImageLoader.getInstance().loadImage(thumbUrl, imageSize, DisplayImageOptionsManager.getInstance().getDefaultOptions(), new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String s, View view) {

            }

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {

            }

            @Override
            public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                WXMediaMessage mediaMessage = new WXMediaMessage(miniProgramObject);
                mediaMessage.setThumbImage(bitmap);
                mediaMessage.title = title;

                SendMessageToWX.Req req = new SendMessageToWX.Req();
                req.message = mediaMessage;
                req.scene = mShareScene;
                api.sendReq(req);

            }

            @Override
            public void onLoadingCancelled(String s, View view) {

            }
        });

    }

    public void shareImage(String imageUrl) {
        ImageLoader.getInstance().loadImage(imageUrl, DisplayImageOptionsManager.getInstance().getDefaultOptions(), new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String s, View view) {

            }

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {

            }

            @Override
            public void onLoadingComplete(String s, View view, Bitmap imageBitmap) {
                WXImageObject imageObject = new WXImageObject(imageBitmap);
                WXMediaMessage mediaMessage = new WXMediaMessage();
                mediaMessage.mediaObject = imageObject;
//                Bitmap thumb = Bitmap.createScaledBitmap(imageBitmap, 150, 150, true);
//                imageBitmap.recycle();
//                mediaMessage.thumbData = bmpToByteArray(imageBitmap, true);

                SendMessageToWX.Req req = new SendMessageToWX.Req();
                req.message = mediaMessage;
                req.transaction = buildTransaction("img");
                req.scene = mShareScene;
                api.sendReq(req);
            }

            @Override
            public void onLoadingCancelled(String s, View view) {

            }
        });
    }

    public void shareImage(Bitmap imageBitmap) {
        WXImageObject imageObject = new WXImageObject(imageBitmap);
        WXMediaMessage mediaMessage = new WXMediaMessage();
        mediaMessage.mediaObject = imageObject;
        Bitmap thumb = Bitmap.createScaledBitmap(imageBitmap, 150, 150, true);
        imageBitmap.recycle();
        mediaMessage.thumbData = bmpToByteArray(thumb, true);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.message = mediaMessage;
        req.transaction = buildTransaction("img");
        req.scene = mShareScene;
        api.sendReq(req);


    }

    private byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
        if (needRecycle) {
            bmp.recycle();
        }

        byte[] result = output.toByteArray();
        try {
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }
}
